package com.express.service.impl;

import com.express.core.mapper.OrdersMapper;
import com.express.emnus.OrderStatusEnum;
import com.express.entity.UserReceipt;
import com.express.mybatis.PageResult;
import com.express.service.IRoutingInfosService;
import com.express.service.UserReceiptService;
import com.express.sys.mapper.UserRectiptMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 28014
 */
@Service
public class UserReceiptServiceImpl implements UserReceiptService {

    private static final Logger log = LoggerFactory.getLogger(UserReceiptServiceImpl.class);

    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    UserRectiptMapper userRectiptMapper;
    @Autowired
    IRoutingInfosService routingInfosService;

    @Override
    public PageResult query(PageResult page) {
        PageResult result = null;
        try {
            String oddNumber = page.getMessage();
            int pageNumber = page.getPageNum();
            int pageSize = page.getPageSize();
            PageHelper.startPage(pageNumber, pageSize);
            Map<String, Object> map = new HashMap<>();
            if (StringUtils.isNotBlank(oddNumber)) {
                map.put("keyword", oddNumber);
            }
            if (StringUtils.isNotBlank(page.getStatus())) {
                map.put("orderStatus", OrderStatusEnum.getOrderStatus(page.getStatus()));
            }
//            map.put("oddNumber","11");
            List<UserReceipt> list = userRectiptMapper.queryList(map);
            result = new PageResult(list);
        } catch (Exception e) {
            log.error("查询失败", e);
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    @Transactional
    public int save(UserReceipt userReceipt) {
//        UserOrders order = ordersMapper.getOrderByOrderNo(userReceipt.getOrderNo());
        int result = 0;
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("oddNumber", userReceipt.getOddNumber());
            String infos = userReceipt.getRoutingInfo();
            List<UserReceipt> userReceipts = userRectiptMapper.queryList(map);
            if (userReceipts != null && userReceipts.size() > 0) {
                if (userReceipt.getHandStr().equals("update")) {
                    log.info("更新信息");
                    result = 1;
                    userReceipt.setRoutingInfo("");
                    userRectiptMapper.update(userReceipt);
                } else {
                    log.error("｛｝ 订单号重复了", userReceipt.getOrderNo());
                    return result;
                }
                userReceipt.setRoutingInfo(infos);
            } else {
                String routingInfo = userReceipt.getRoutingInfo();
                userReceipt.setRoutingInfo("");
                result = userRectiptMapper.save(userReceipt);
                userReceipt.setRoutingInfo(routingInfo == null ? "" : routingInfo);
            }
            routingInfosService.saveRouting(userReceipt.getId(), userReceipt.getRoutingInfo());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }


    @Override
    public int deleteByIds(int[] ids) {
        return userRectiptMapper.deleteByIds(ids);
    }

    @Override
    public int delete(UserReceipt userReceipt) {
        return userRectiptMapper.delete(userReceipt.getId());
    }

    @Override
    public int update(UserReceipt userReceipt) {
        return userRectiptMapper.update(userReceipt);
    }

    @Override
    public UserReceipt getById(int id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        List<UserReceipt> userReceipts = userRectiptMapper.queryList(map);
        if (userReceipts != null && userReceipts.size() > 0) {
            return userReceipts.get(0);
        }
        return null;
    }

}
